<template>
  <div>
    <common-head title="列表页"/>
    <!-- 排序 -->
    <div class="filter">
      <div @click="orderBySale" :class="['sale','order', {active: order.by === 'sale'}]">
        <span>销量</span>
        <div :class="['angle','up', {active:order.by === 'sale'&& order.upDown === 'up'}]"></div>
        <div :class="['angle','down', {active:order.by === 'sale'&& order.upDown === 'down'}]"></div>
      </div>
      <div @click="orderByPrice" :class="['price','order', {active: order.by === 'price'}]">
        <span>价格</span>
        <div :class="['angle','up', {active:order.by === 'price'&& order.upDown === 'up'}]"></div>
        <div :class="['angle','down', {active:order.by === 'price'&& order.upDown === 'down'}]"></div>
      </div>
    </div>
    <!-- /排序 -->
    <!-- 楼层 -->
    <div class="recommend">
      <div class="zanwu" v-if="!items.length">暂无商品</div>
    <div class="recommend-main">
      <div class="item" @click="enterDetail(item.id)" v-for="item in items" :key="item.id">
        <img :src="item.pic" alt="">
        <div class="title">
          {{item.name}}
        </div>
        <div class="item-action">
          <div class="price">
            {{ item.minPrice | currency('￥') }}
          </div>
          <van-icon @click.stop="addCart(item)" size="20" class-prefix="icon" name="jia"/>
        </div>
      </div>
    </div>
  </div>
    <!-- /楼层 -->
  </div>
</template>

<script>

import { fetchItems } from '@api'
import CommonHead from '@components/CommonHead.vue'
import { currency } from '@/filters'
import addCart from '@mixins/addCart'
export default {
  mixins: [addCart],
  data () {
    return {
      items: [],
      orderBy: 'ordersDown',
      order: {
        by: 'sale', // sale price
        upDown: 'down'
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    console.log(to.meta.deep)
    console.log(from.meta.deep)
    if (from.meta.deep > to.meta.deep) {
      // 从首页到列表页
      to.meta.keepAlive = false
    } else {
      // 从详情页到列表页
      to.meta.keepAlive = true
    }
    next()
  },
  created () {
    this.fetchItems()
  },
  methods: {

    enterDetail (id) {
      // 进入详情页
      this.$router.push({
        path: '/detail',
        query: {
          itemId: id
        }
      })
    },
    orderBySale () {
      // 点击按照销量排序
      // 情况1 从按照价格排序 点击按照销量排序
      if (this.order.by === 'price') {
        // 切换销量 降序
        this.order.by = 'sale'
        this.order.upDown = 'down'
        this.orderBy = 'ordersDown'
      } else {
        // 情况2 在销量升序和降序之间切换
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'ordersDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'ordersUp'
        }
      }

      // 重新请求 来排序
      this.fetchItems()
    },
    orderByPrice () {
      // 点击按价格排序
      // 情况1 从按照销量排序 点击按照价格排序
      if (this.order.by === 'sale') {
        // 切换价格升序
        this.order.by = 'price'
        this.order.upDown = 'up'
        this.orderBy = 'priceUp'
      } else {
        // 情况2 在价格升序和降序之间切换
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'priceDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'priceUp'
        }
      }
      // 重新请求 来排序
      this.fetchItems()
    },
    fetchItems () {
      // 请求列表数据
      fetchItems({
        categoryId: this.$route.params.cate_id,
        orderBy: this.orderBy
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    }
  },
  filters: {
    currency
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>

.filter{
  height: 40px;
  width: 100%;
  position: fixed;
  left:0;
  top:46px;
  display: flex;
  background: #fff;
  align-items: center;
  font-size: 12px;
  color:#333;
  justify-content: space-around;
  border-bottom: 1px solid #999;
  .order{
    &.active{
      span{
        color:#f66;
      }
    }
    position: relative;
    .angle{
      width:0;
      height:0;
      border: 6px solid transparent;
      position: absolute;
      right: -16px;
      &.up{
        &.active{
          border-bottom-color: #f66;
        }
        border-bottom-color: #333;
        top:-4px;
      }
      &.down{
        &.active{
          border-top-color: #f66;
        }
        border-top-color: #333;
        top:10px;
      }
    }
  }
}
.recommend{
  margin-top: 86px;
  &-main{
    display: flex;
    flex-wrap: wrap;
    .item{
      width: 50%;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 0 10px;
      box-sizing: border-box;
      img{
        width: 100%;
      }
      .title{
        width: 100%;
        line-height: 30px;
        color:#333;
        font-size: 14px;
        font-weight: 900;
        overflow:hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      &-action{
        width: 100%;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color:#f66;
      }
    }
  }
}
</style>
